<?php
// 获取用户名、密码、简介
$uid = trim($_POST['uid'])==""?die("<a href='#' onclick='window.history.back()'>必须输入uid</a>"):trim($_POST['uid']);
//$username = trim($_POST['userName'])==""?die("<a href='#' onclick='window.history.back()'>必须输username</a>"):trim($_POST['userName']);
$userPwd = trim($_POST['userPwd'])==""?die("<a href='#' onclick='window.history.back()'>必须输入userPwd</a>"):trim($_POST['userPwd']);
$note = trim($_POST['note'])==""?die("<a href='#' onclick='window.history.back()'>必须输入note</a>"):trim($_POST['note']);
$profile = $_POST['oldProfile'];

if($_FILES['profile']['size']>0){
    if(stripos($_FILES["profile"]["type"],'image') !== false){
        $day =  date("y-m-d");
        // 存放路径
        $path = "./uploads/$day";
        // 创建目录
        if(!is_dir($path))  mkdir($path);
        // 生成文件名
        $fileName = uniqid().$_FILES["profile"]["name"];
        if(move_uploaded_file($_FILES["profile"]["tmp_name"],$path."/".$fileName)){
            // 判断是否是默认图片
            if($profile != "uploads/head.png"){
                uniqid($profile);
            }

            $profile = $path."/".$fileName;
        }
        uniqid($_FILES["profile"]["tmp_name"]);
    }
}

/*
 * 更新信息
 */
$sql = "update userinfo set note=?,profile=?,userPwd=? where uid=?";
$db = mysqli_connect("localhost", "root","123456","bbs");
$pr = $db->prepare($sql);
$pr->bind_param("sssi",$note,$profile,$userPwd,$uid);
if($pr->execute()){
    $sql = "select * from userinfo where uid=? ";
    $pre = $db->prepare($sql);  // 预编译
    /*绑定占位符数据,
        第一个参数是参数数量和类型说明，一个字母代表一个参数，s:字符串；i：int； b：大数据
        后面几个参数只能是变量不能是具体值
    */
    $pre->bind_param("i",$uid);
// 执行预编译sql
    $pre->execute();
// 获取结果集，参数传存结果列的变量
    $pre->bind_result($uid,$userName,$userPwd,$note,$profile,$isadmin);
//// 获取结果集中的一行数据
//$pre->fetch();
//echo "$uid,$userName,$userPwd,$note";
//4、输出结果
    if($pre->fetch()){
        $userinfo = array();
        $userinfo['userName'] = $userName;
        $userinfo['userPwd'] = $userName;
        $userinfo['note'] = $note;
        $userinfo['profile'] = $profile;
        $userinfo['isadmin'] = $isadmin;
        $userinfo['uid'] = $uid;
//    开启session
        session_start();
//    把用户信息粗难道session
        $_SESSION['userinfo'] = $userinfo;
    }
    header("location:index.php");
}else{
    echo $pr->error_list;
    echo "修改失败";
}